package com.mgg.payment.service.order.impl;

import java.math.BigDecimal;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import com.mgg.common.dao.DaoSupport;
import com.mgg.common.dto.DeliveryOrderDetailDTO;
import com.mgg.common.dto.OrderDetailDTO;
import com.mgg.common.exception.ServiceException;
import com.mgg.payment.enums.PaymentResultEnum;
import com.mgg.payment.service.order.DeliveryOrderService;
import com.mgg.payment.util.OrderCodeUtils;
import org.apache.commons.collections.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;


@Service("deliveryOrderService")
public class DeliveryOrderServiceImpl implements DeliveryOrderService {

	@Resource(name = "daoSupport")
	private DaoSupport dao;
	
	@Autowired
	private StringRedisTemplate redisTemplate;

	/**
	 * 创建配送单
	 */
	@SuppressWarnings("unchecked")
	@Override
	@Transactional
	public void createDeliveryOrder(String orderId, String payTime) throws Exception {
		Map<String, Object> insertMap = new HashMap<>();
		String doCode = OrderCodeUtils.generateOrderCode("2", redisTemplate);
		insertMap.put("doCode", doCode);
		insertMap.put("soCode", orderId);
		insertMap.put("isDeliveryPay", false);
		insertMap.put("payTime", payTime);
		Integer count = dao.save("DeliveryOrderMapper.insertDeliveryOrderBySO", insertMap);
		if (count == 0) {
			throw new ServiceException(PaymentResultEnum.ORDER_DO_SAVE_ERROR.getCode(),PaymentResultEnum.ORDER_DO_SAVE_ERROR.getMessage());
		}

		List<OrderDetailDTO> soDetails = dao.findForList("SalesOrderMapper.listOrderDetailListByOrderId",orderId);

		for (OrderDetailDTO soDetail : soDetails) {
			List<DeliveryOrderDetailDTO> doDetails = getCombinationGoods(soDetail.getSpecId());
			int goodsNum = soDetail.getGoodsCount();
			if (CollectionUtils.isNotEmpty(doDetails)) {
				for (DeliveryOrderDetailDTO doDetail : doDetails) {
					insertDeliveryOrderDetail(doCode, doDetail.getGoodsId(), doDetail.getSpecId(), doDetail.getSku(),
							goodsNum * doDetail.getQty(), doDetail.getPrice());
				}
			} else {
				insertDeliveryOrderDetail(doCode, soDetail.getGoodsId(), soDetail.getSpecId(), soDetail.getBarcode(),
						goodsNum, soDetail.getGoodsPrice());
			}
		}
	}
	
	@Override
	public boolean isWmsDo(String soCode) throws Exception {
		return (int) dao.findForObject("DeliveryOrderMapper.getOrderWmsGoodsCount", soCode) > 0 ? true : false;
	}

	/**
	 * 插入wmsorder详情
	 * @param doCode
	 * @param goodsId
	 * @param specId
	 * @param sku
	 * @param qty
	 * @param price
	 * @throws Exception
	 */
	private void insertDeliveryOrderDetail(String doCode, String goodsId, String specId, String sku, Integer qty,
			BigDecimal price) throws Exception {
		Map<String, Object> insertMap = new HashMap<>();
		insertMap.put("doCode", doCode);
		insertMap.put("goodsId", goodsId);
		insertMap.put("specId", specId);
		insertMap.put("sku", sku);
		insertMap.put("qty", qty);
		insertMap.put("price", price);
		Integer count = dao.save("DeliveryOrderMapper.insertDeliveryOrderDetail", insertMap);
		if (count == 0 ) {
			throw new ServiceException(PaymentResultEnum.ORDER_DO_DTLS_SAVE_ERROR.getCode(),PaymentResultEnum.ORDER_DO_DTLS_SAVE_ERROR.getMessage());
		}
	}

	private List<DeliveryOrderDetailDTO> getCombinationGoods(String specId) throws Exception {
	    List<DeliveryOrderDetailDTO> list = dao.findForList("DeliveryOrderMapper.getCombinationGoods", specId);
		return list;
	}	
}
